package org.rowan.infovis.soccer;

import com.sun.opengl.util.GLUT;
import com.sun.opengl.util.j2d.TextRenderer;
import java.awt.Color;
import java.awt.Font;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;

public class Goal {

    private float HEIGHT = 300f;
    private float WIDTH = 50f;
    private boolean precision;
    private double value;

    public Goal(boolean precision, double value) {
        this.precision = precision;
        this.value = value;
    }

    public float getHeight() {
        return HEIGHT;
    }

    public float getWidth() {
        return WIDTH;
    }

    public void draw(GL gl, GLAutoDrawable drawable, FieldViewer fv) {
        gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);

        gl.glColor3f(1f, 1f, 1f);
        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(0f, 0f);
        gl.glVertex2f(0f, HEIGHT);
        gl.glVertex2f(WIDTH, HEIGHT);
        gl.glVertex2f(WIDTH, 0f);
        gl.glEnd();

        gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL);

        if (precision) {
            fillPrecisionBar(gl);
        }

                System.out.println(value + " " + precision);
        gl.glPushMatrix();
        gl.glTranslated(-50.0, 0.0, 0.0);
        if (precision) {
            drawLabel(gl, drawable, fv);
        }
        gl.glPopMatrix();
    }

    private void drawLabel(GL gl, GLAutoDrawable drawable, FieldViewer fv) {
        TextRenderer tr = new TextRenderer(new Font("SansSerif", Font.PLAIN, 18));
        System.out.println(value);

        if (value >= 0.0 && value <= 25.0) {
            tr.setColor(Color.RED);
        } else if (value > 25.0 && value <= 50.0) {
            tr.setColor(1f, 0.563f, 0f, 1f);
        } else if (value > 50.0 && value <= 75.0) {
            tr.setColor(Color.YELLOW);
        } else if (value > 75.0 && value <= 100.0) {
            tr.setColor(Color.GREEN);
        }

        tr.beginRendering(drawable.getWidth(), drawable.getHeight());
        tr.draw("P", (int) (fv.FIELD_X - WIDTH * 2 + 20), (int) (fv.FIELD_Y + HEIGHT + 20));
        tr.draw("R", (int) (fv.FIELD_X - WIDTH * 2 + 20), (int) (fv.FIELD_Y + HEIGHT));
        tr.draw("E", (int) (fv.FIELD_X - WIDTH * 2 + 20), (int) (fv.FIELD_Y + HEIGHT - 20));
        tr.draw("C", (int) (fv.FIELD_X - WIDTH * 2 + 20), (int) (fv.FIELD_Y + HEIGHT - 40));
        tr.draw("I", (int) (fv.FIELD_X - WIDTH * 2 + 25), (int) (fv.FIELD_Y + HEIGHT - 60));
        tr.draw("S", (int) (fv.FIELD_X - WIDTH * 2 + 20), (int) (fv.FIELD_Y + HEIGHT - 80));
        tr.draw("I", (int) (fv.FIELD_X - WIDTH * 2 + 25), (int) (fv.FIELD_Y + HEIGHT - 100));
        tr.draw("O", (int) (fv.FIELD_X - WIDTH * 2 + 20), (int) (fv.FIELD_Y + HEIGHT - 120));
        tr.draw("N", (int) (fv.FIELD_X - WIDTH * 2 + 20), (int) (fv.FIELD_Y + HEIGHT - 140));
        tr.endRendering();
    }

    private void fillPrecisionBar(GL gl) {
        float bar = (float) (value / 100) * WIDTH;

        if (value >= 0.0 && value <= 25.0) {
            gl.glColor3f(1f, 0f, 0f);
        } else if (value > 25.0 && value <= 50.0) {
            gl.glColor3f(1f, 0.563f, 0f);
        } else if (value > 50.0 && value <= 75.0) {
            gl.glColor3f(1f, 1f, 0f);
        } else if (value > 75.0 && value <= 100.0) {
            gl.glColor3f(0f, 1f, 0f);
        }

        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(WIDTH, 0.0f);
        gl.glVertex2f(WIDTH - bar, 0.0f);
        gl.glVertex2f(WIDTH - bar, HEIGHT);
        gl.glVertex2f(WIDTH, HEIGHT);
        gl.glEnd();
    }
}
